package com.abkj.platform.tail.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.abkj.platform.base.dao.warp.SQLType;
import com.abkj.platform.tail.cons.CommandType;
import com.abkj.platform.tail.domain.Displacement;
import com.abkj.platform.tail.domain.Level;
import com.abkj.platform.tail.domain.Osmometer;
import com.abkj.platform.tail.domain.Rain;
import com.abkj.platform.tail.domain.Ultrasonic;
import com.abkj.platform.tail.service.SerialService;

@Controller
@RequestMapping({ "sensor" })
public class SensorController extends BaseController {

	@Autowired
	SerialService serialService;

	@RequestMapping({ "/osmometer" })
	public String osmometer_list(ModelMap model) {
		List osmometers = this.baseService.findAllObjects(Osmometer.class);
		System.out.println(osmometers);
		model.put("osmometers", osmometers);
		return "/sensor/osmometer/osmometer";
	}

	@RequestMapping({ "/osmometer_add" })
	public String osmometer_add() {
		return "/sensor/osmometer/osmometer_add";
	}

	@RequestMapping({ "/reset/{code}" })
	@ResponseBody
	public String reset(@PathVariable String code) {
		this.serialService.resetSerial(code);
		return "0";
	}

	@RequestMapping({ "/zero/{code}/{type}/{sid}/{table}" })
	@ResponseBody
	public String zeroOrRemove(HttpServletRequest request, @PathVariable String code, @PathVariable String table,
			@PathVariable int sid, @PathVariable int type) {
		if (type == 0) {
			createJcUserLog(request, "调零", "对 " + table + " " + sid + " 执行调零操作");
		}
		String i = this.serialService.zeroOrRemove(code, type == 0 ? CommandType.ZERO : CommandType.CLEAR);
		if ((type == 0) && (i.equals("0")) && (table.equals("osmometer"))) {
			List maps = this.baseService.findBySql("select * from " + table + " where id=" + sid);
			if (maps.size() > 0) {
				Map map = (Map) maps.get(0);
				if (table.equals("osmometer")) {
					float off = NumberUtils.toFloat(String.valueOf(map.get("offer")));
					if (off != 0.0F) {
						float old = NumberUtils.toFloat(String.valueOf(map.get("depth")));
						float depth = old - off * 100.0F / 9.8F;
						this.baseService.excuteBySql(SQLType.UPDATE,
								"update " + table + " set depth=" + depth + " where id = " + sid);
					}
				}
			}
		}

		return i;
	}

	@RequestMapping({ "/osmometer_update" })
	public String osmometer_edit(ModelMap model,
			@RequestParam(value = "osmometerId", defaultValue = "-1") int osmometerId) {
		System.out.println(osmometerId);
		Osmometer osmometer = (Osmometer) this.baseService.loadObjectById(Osmometer.class, osmometerId);
		System.out.println(osmometer);
		model.put("osmometer", osmometer);
		return "/sensor/osmometer/osmometer_update";
	}

	@RequestMapping({ "/osmometer_saveOrUpdate" })
	public String osmometer_saveOrUpdate(Osmometer osmometer) {
		Integer id = osmometer.getId();
		if ((id != null) && (id.intValue() > 0))
			this.baseService.updateObject(osmometer, "code,name,pxy,state,num,sectdes,depth,alarm");
		else {
			this.baseService.insertObject(osmometer);
		}
		return "redirect:osmometer";
	}

	@RequestMapping({ "/level" })
	public String level_list(ModelMap model) {
		List levels = this.baseService.findAllObjects(Level.class);
		model.put("levels", levels);
		return "/sensor/level/level";
	}

	@RequestMapping({ "/level_add" })
	public String level_add() {
		return "/sensor/level/level_add";
	}

	@RequestMapping({ "/level_update" })
	public String level_edit(ModelMap model, @RequestParam(value = "levelId", defaultValue = "-1") int levelId) {
		Level level = (Level) this.baseService.loadObjectById(Level.class, levelId);
		model.put("level", level);
		return "/sensor/level/level_update";
	}

	@RequestMapping({ "/level_saveOrUpdate" })
	public String level_saveOrUpdate(Level level) {
		Integer id = level.getId();
		if ((id != null) && (id.intValue() > 0))
			this.baseService.updateObject(level, "code,name,pxy,state,num,type,alertup,alertdown");
		else {
			this.baseService.insertObject(level);
		}
		return "redirect:level";
	}

	@RequestMapping(value = { "level_init" }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String level_init(int id) {
		String sql = "update level set init=should where id=" + id;
		this.baseService.excuteBySql(SQLType.UPDATE, sql);
		return "0";
	}

	@RequestMapping({ "/ultrasonic" })
	public String ultrasonic_list(ModelMap model) {
		Ultrasonic ultrasonic = (Ultrasonic) this.baseService.loadObjectById(Ultrasonic.class, 1);
		model.put("ultrasonic", ultrasonic);
		return "/sensor/ultrasonic/ultrasonic";
	}

	@RequestMapping({ "/ultrasonic_saveOrUpdate" })
	public String ultrasonic_saveOrUpdate(Ultrasonic ultrasonic) {
		Integer id = ultrasonic.getId();
		if ((id != null) && (id.intValue() > 0))
			this.baseService.updateObject(ultrasonic, "code,name,pxy,state,len,lenh,height,level1,level2");
		else {
			this.baseService.insertObject(ultrasonic);
		}
		return "redirect:ultrasonic";
	}

	@RequestMapping({ "/rain" })
	public String rain_list(ModelMap model) {
		Rain rain = (Rain) this.baseService.loadObjectById(Rain.class, 1);
		model.put("rain", rain);
		return "/sensor/rain/rain";
	}

	@RequestMapping({ "/rain_saveOrUpdate" })
	public String rain_saveOrUpdate(Rain rain) {
		Integer id = rain.getId();
		if ((id != null) && (id.intValue() > 0))
			this.baseService.updateObject(rain, "code,name,pxy,state,alertval");
		else {
			this.baseService.insertObject(rain);
		}
		return "redirect:rain";
	}
	
	@RequestMapping({ "/displacement" })
	public String displacement_list(ModelMap model) {
		List displacements = this.baseService.findAllObjects(Displacement.class);
		model.put("displacements", displacements);
		return "/sensor/displacement/displacement";
	}

	@RequestMapping({ "/displacement_add" })
	public String displacement_add() {
		return "/sensor/displacement/displacement_add";
	}

	@RequestMapping({ "/displacement_update" })
	public String displacement_edit(ModelMap model, @RequestParam(value = "displacementId", defaultValue = "-1") int displacementId) {
		Displacement displacement = (Displacement) this.baseService.loadObjectById(Displacement.class, displacementId);
		model.put("displacement", displacement);
		return "/sensor/displacement/displacement_update";
	}

	@RequestMapping({ "/displacement_saveOrUpdate" })
	public String displacement_saveOrUpdate(Displacement displacement) {
		Integer id = displacement.getId();
		if ((id != null) && (id.intValue() > 0))
			this.baseService.updateObject(displacement, "code,name,pxy,state,num,type,alertup,alertdown");
		else {
			this.baseService.insertObject(displacement);
		}
		return "redirect:displacement";
	}

	@RequestMapping(value = { "displacement_init" }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String displacement_init(int id) {
		String sql = "update displacement set init=should where id=" + id;
		this.baseService.excuteBySql(SQLType.UPDATE, sql);
		return "0";
	}
}